//
//  FLLWorksDetailViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/19.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLWorksDetailViewController.h"
#import "FLLMacro.h"
#import "FLLWorksModel.h"
#import "FLLWorksDetailTableViewCell.h"
#import "FLLWorksMoreTableViewCell.h"
#import "FLLCommentTableViewCell.h"
#import "FLLSpeakTableViewCell.h"
#import "WorksData.h"
#import "WorksLike.h"
#import "WorksTopics.h"
#import "WorksRelates.h"

@interface FLLWorksDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, copy) NSString *bodyString;
@property (nonatomic, strong) FLLWorksModel *fllWorksModel;
@property (nonatomic, strong) UITableView *worksDetailTableView;
@property (nonatomic, strong) WorksData *infoData;
@property (nonatomic, strong) NSMutableArray<WorksLike *> *agreeCollectionData;
@property (nonatomic, strong) NSMutableArray<WorksRelates *> *moreWorksCollectionData;
@property (nonatomic, copy) NSString *topicName;
@end

@implementation FLLWorksDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"作品详情";
    [self createPOSTRequest];
    [self createWorksDetailTableView];
}
- (void)createPOSTRequest
{
    
    self.bodyString = [NSString stringWithFormat:@"post_id=%@&methodName=ShequPostview&version=4.40&", _idString];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:self.bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.fllWorksModel = [FLLWorksModel modelWithDic:result];
        self.infoData = self.fllWorksModel.data;
        self.topicName = self.fllWorksModel.data.topics[0].topic_name;
        self.agreeCollectionData = (NSMutableArray *)self.fllWorksModel.data.like;
        self.moreWorksCollectionData = (NSMutableArray *) self.fllWorksModel.data.relates;
        [self.worksDetailTableView reloadData];
    
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (void)createWorksDetailTableView
{

    self.worksDetailTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.worksDetailTableView.delegate = self;
    self.worksDetailTableView.dataSource = self;
    [self.view addSubview:_worksDetailTableView];
    [self.worksDetailTableView registerNib:[UINib nibWithNibName:@"FLLWorksDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"worksDetailTableCell"];
    [self.worksDetailTableView registerNib:[UINib nibWithNibName:@"FLLWorksMoreTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"worksMoreTableCell"];
    [self.worksDetailTableView registerNib:[UINib nibWithNibName:@"FLLCommentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"commentTableCell"];
    [self.worksDetailTableView registerNib:[UINib nibWithNibName:@"FLLSpeakTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"speakTableCell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0) {
        return 3;
    }
    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            FLLWorksDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"worksDetailTableCell"];
            cell.contentLabel.text = self.infoData.content;
            [cell.userPhoto sd_setImageWithURL:[NSURL URLWithString:_infoData.head_img] placeholderImage:[UIImage imageNamed:@"userHeadImage-1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
            }];
            [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_infoData.image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
            }];
            if (self.infoData.istalent == 0) {
                cell.specialIcon.backgroundColor = [UIColor clearColor];
                cell.specialIcon.image = [UIImage new];
            } else {
            
                cell.specialIcon.image = [UIImage imageNamed:@"达人"];
            }
            cell.userName.font = [UIFont systemFontOfSize:15 * kWidthScale];
            cell.contentLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
            cell.timeLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
            cell.userName.text = self.infoData.nick;
            cell.timeLabel.text = self.infoData.create_time;
             
            return cell;
        }
        if (indexPath.row == 1) {
            FLLWorksMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"worksMoreTableCell"];
          
            if ([self.infoData.agree_count isEqualToString:@""]) {
                cell.agreeLabel.text = [NSString stringWithFormat:@"美图不错,点个赞"];
            } else {
            cell.agreeLabel.text = [NSString stringWithFormat:@"%@位厨友觉得很赞", self.infoData.agree_count];
            }
            cell.agreeLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
            cell.moreWorksLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
            cell.moreWorksLabel.text = [NSString stringWithFormat:@"更多关于%@的作品", self.topicName];
            cell.agreeCollectionData = self.agreeCollectionData;
            cell.moreWorksCollectionData = self.moreWorksCollectionData;
            
            
            cell.moreBlock = ^(NSString *idString){

                FLLWorksDetailViewController *anotherVC = [[FLLWorksDetailViewController alloc] init];
                anotherVC.idString = idString;
                [self.navigationController pushViewController:anotherVC animated:YES];
            };
            
            
            
            
            return cell;
        } if (indexPath.row == 2) {
            FLLCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentTableCell"];
            cell.commentLabel.text = @"抢先评论上头条";
            cell.commentLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
            cell.commentButton.backgroundColor = FLLOrangeColor;
            cell.commentButton.titleLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
            return cell;
        }
    }
    FLLSpeakTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"speakTableCell"];
    return cell;
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return SCREEN_WIDTH * (1 + 2 / 6.0 );
        } if (indexPath.row == 1) {
            return SCREEN_WIDTH * 6 / 7.0;
        } if (indexPath.row == 2) {
            return SCREEN_WIDTH / 4.0 + 20 * kHeightScale;
        }
    }

        return SCREEN_WIDTH / 4.0;
    
}

- (void)setIdString:(NSString *)idString
{

    _idString = idString;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
